package com.learn.dept.service;

import com.learn.dao.DeptDao;
import com.learn.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;
    
    @Override
    public String addDept(String name) {
        if (StringUtils.isEmpty(name)) {
            return "部门名称不能为空";
        }
        
        deptDao.save(new Dept().setName(name).setDbSource("learn_cloud_8002"));
        return "添加成功";
    }
    
    @Override
    public Dept findOnedept(Integer id) {
        Optional<Dept> deptOptional = deptDao.findById(id);
        return deptOptional.orElse(null);
    }
    
    @Override
    public List<Dept> findAllDept() {
        return deptDao.findAll();
    }
}
